<template>
	<view class="content">
		<view v-if="blogs.length == 0" class="none">
			<image src="../../static/icons/nodata.png" mode="widthFix" class="logo"></image>
			<text>抱歉，没有相关文章</text>
		</view>

		<view v-else class="item-container" v-for="(item,index) in blogs" @click="gotoDetail(item.id)">
			<text class="iconfont icon-arrow-down-filling img-oper"></text>
			<text class="item-title">{{item.title}}</text>

			<rich-text :nodes="item.description" class="item-desc"></rich-text>
			<image :src="item.picture" mode="widthFix" class="img item-pic"></image>

			<text class="iconfont icon-arrow-down-filling img-oper" @click="popSheet"></text>
			<view>
				<text v-for="(tag,idx) in item.tags" class="item-tag">{{tag}}</text>
			</view>
			<image :src="item.user.avatar" mode="aspectFill" class="img-head"></image>
			<view class="icon-container">
				<text @click.stop="onChangeGood(item.id,true)" v-if="good_ids.indexOf(item.id)<0"
					class="iconfont icon-dianzan icon"></text>
				<text @click.stop="onChangeGood(item.id,false)" v-else class="iconfont icon-dianzan1 icon"></text>
				<text class="icon-text">{{item.good}}</text>
				<text class="iconfont icon-pinglun icon"></text>



				<text class="icon-text">{{item.comments.length}}</text>
				<text class="iconfont icon-icon- icon"></text>
				<text class="icon-text">{{item.readCount}}</text>
			</view>
		</view>
		<view class="bottom" v-if="blogs.length == count">
			---我是有底线的---
		</view>

		<dialog-shell ref="shell" title="用户信息" @confirm="confirmInfo">
			<text style="padding: 10rpx; font-size: 28rpx;">{{userDesc}}</text>

		</dialog-shell>

	</view>

</template>

<script>
	let page = 0 //当前页
	let size = 3 //每一页的大小
	let key_blogs = "abc";
	let key_goods = "goods";
	export default {
		data() {
			return {
				blogs: [], //请求的数据存放的数组
				count: -1, //用于保存服务器返回的总数据条数
				searching: false,
				searching: false,
				//到达底部提示
				reachBottomTip: '',
				good_ids: [],
				userDesc: ""
			}
		},

		onLoad() {
			this.$noti.add(this.$params.noti_refresh_count, this.notifyRefreshCount, this)
			let ids = uni.getStorageSync(key_blogs)

			if (ids) {
				this.good_ids = ids
			}
			// 尝试调用本地数据本地缓存
			let blogs = uni.getStorageSync(this.$params.key_good_ids)
			if (blogs) {
				//存在时使用本地数据
				this.blogs = blogs
				this.count = blogs.length
			} else {
				//不存在时使用本地数据
				this.getPagedBlogs()
			}
			// console.log(this.blogs);


		},

		onPullDownRefresh() {
			page = 0
			this.blogs = []
			this.count = -1
			this.getPagedBlogs()
		},
		onReachBottom() {
			if (this.searching || this.blogs.length == this.count) {
				return
			}
			page += 1
			this.getPagedBlogs()
		},
		methods: {
			showUserInfo(user) {
				this.$refs.shell.show();
				this.userDesc = "作者" + user.nickName + "\n联系方式" + user.email;
			},
			confirmInfo() {
				console.log("emit");
			},
			gotoDetail(id) {
				uni.navigateTo({
					url: '../blog/blog?id=' + id,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},

			refreshReadCount(id, count) {
				this.blogs.forEach(b => {
					if (b.id == id) {
						b.readCount = count
					}
				})
			},
			onChangeGood(id, isGood) {
				//todo:同步到服务器，并且取最新数据
				let url = this.$params.host
				let action = isGood ? this.$params.action_good : this.$params.action_del_good
				url += action
				let data = {
					"id": id
				}
				console.log(url);
				this.$request.postParam(url, data, res => {
					this.blogs.forEach(blog => {
						if (blog.id == id) {
							blog.good = res.data
						}
					})
					let blog_data = {
						"picture": b.picture,
						"id": b.id,
						"title": b.title
					}
					// console.log(this.good_ids);
					// console.log(b.id);

					// console.log(this.good_ids.findIndex(e => e.id == b.id));
					if (isGood) {
						this.good_ids.push(blog_data);
					} else {
						this.good_ids.splice(this.good_ids.findIndex(e => e.id == b.id), 1);
					}
					uni.setStorageSync(this.$params.key_good_ids, this.good_ids)


				}, () => {})
			},
			getPagedBlogs() {
				console.log("开始分页请求")

				if (this.blogs.length == this.count) {
					uni.showToast({
						title: "加载完成"
					})
					return
				}
				let header = {
					"content-type": this.$params.content_type,
					"page": page,
					"size": size
				}
				let url = this.$params.host + this.$params.action_page;
				console.log(url);
				this.$request.getWithHeader(url, header, res => {
					res.data.forEach(blog => {
						if (!blog.picture.startsWith("http")) {
							blog.picture = this.$params.host + blog.picture

							// blog.user.avatar = this.$params.host + blog.user.avatar
						}
						this.blogs = [...this.blogs, ...res.data];
						this.count = parseInt((res.message));
						console.log(111);
					});
				}, () => {
					uni.stopPullDownRefresh();
					console.log(this.blogs)
				});
			},
			search(e) {
				let kw = e.value
				if (kw = length > 0) {
					this.blogs = [],
						this.count = -1;
					//发送请求
					this.$request.get(this.$params.host + this.$params.action_blogs_search + kw, res => {
						res.data.forEach(blog => {
							blog.user.avatar = this.$params.host + blog.user.avatar;
						});
						this.blogs = [...this.blogs, ...res.data]
						uni.setStorageSync(key_blogs, this.blogs)
						this.count = parseInt(res.message);
						this.searching = true;
					}, () => {
						console.log("搜索完成");
					});

				}
			},
			clearKeyword(e) {
				//初始化blgos
				this.count = -1;
				this.blogs = [];
				this.searching = false;
				//初始化页码
				this.getPagedBlogs();
			},
			popSheet(id) {
				uni.showActionSheet({
					itemList: ["删除", "收藏"],
					success: res => {
						if (res.tapIndex == 0) {
							uni.showModal({
								title: '删除blog',
								content: '确认删除吗',
								showCancel: true,
								cancelText: '算了',
								confirmText: '删除',
								success: res => {
									if (res.confirm) {
										this.deleteBlog(id)
									}
								},
								fail: () => {},
								complete: () => {}
							});
						} else {
							uni.showToast({
								title: '收藏成功'
							});
						}
					},
					fail: () => {},
					complete: () => {}
				})
			},
			deleteBlog(id) {
				uni.showToast({
					title: '还未登录，不可删除'
				});
			},

			notifyRefreshCount(info) {
				let id = info.id
				let count = info.count
				this.refreshReadCount(id, count)
			},

			refreshReadCount(id, count) {
				this.blogs.forEach(b => {
					if (b.id == id) {
						b.readCount = count
					}
				})
			},



		}
	}
</script>

<style>
	@import url("index.css");
</style>
